package com.ruoyi.exam.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.exam.vo.ExamQuestionBankVo;
import com.ruoyi.exam.vo.WeeklyCheckInVo;
import com.ruoyi.exam.service.IExamQuestionBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.exam.vo.BankProgressInfoVo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 题库Controller
 *
 * @author Tellsea
 * @date 2025-05-30
 */
@Api(tags = "题库Controller")
@RestController
@RequestMapping("/exam/questionBank")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamQuestionBankController extends BaseController {

    private final IExamQuestionBankService examQuestionBankService;

    @ApiOperation("查询题库列表")
    @PreAuthorize("@ss.hasPermi('exam:questionBank:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamQuestionBankVo> list(ExamQuestionBankVo entity) {
        return examQuestionBankService.queryList(entity);
    }

    @ApiOperation("查询题库所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamQuestionBankVo entity) {
        return AjaxResult.success("查询成功", examQuestionBankService.queryAll(entity));
    }

    @ApiOperation("导出题库列表")
    @PreAuthorize("@ss.hasPermi('exam:questionBank:export')")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamQuestionBankVo entity) {
        List<ExamQuestionBankVo> list = examQuestionBankService.queryAll(entity);
        ExcelUtil<ExamQuestionBankVo> util = new ExcelUtil<>(ExamQuestionBankVo.class);
        util.exportExcel(response, list, "题库数据");
    }

    @ApiOperation("获取题库详细信息")
    @PreAuthorize("@ss.hasPermi('exam:questionBank:query')")
    @GetMapping(value = "/getInfo/{bankId}")
    public AjaxResult getInfo(@PathVariable("bankId") Long bankId) {
        return AjaxResult.success("查询成功", examQuestionBankService.queryById(bankId));
    }

    @ApiOperation("新增题库")
    @PreAuthorize("@ss.hasPermi('exam:questionBank:add')")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamQuestionBankVo entity) {
        return toAjax(examQuestionBankService.save(entity));
    }

    @ApiOperation("修改题库")
    @PreAuthorize("@ss.hasPermi('exam:questionBank:edit')")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamQuestionBankVo entity) {
        return toAjax(examQuestionBankService.updateById(entity));
    }

    @ApiOperation("删除题库")
    @PreAuthorize("@ss.hasPermi('exam:questionBank:remove')")
    @Log(title = "题库", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{bankIds}")
    public AjaxResult remove(@PathVariable Long[] bankIds) {
        return toAjax(examQuestionBankService.removeByIds(Arrays.asList(bankIds)) ? 1 : 0);
    }

    @ApiOperation("查询我的题库分类列表")
    @GetMapping("/myCategoryList")
    public AjaxResult myCategoryList(ExamQuestionBankVo entity) {
        return AjaxResult.success("查询成功", examQuestionBankService.myCategoryList(entity, getUserId()));
    }

    @ApiOperation("查询我的题库列表")
    @GetMapping("/myQuestionBankList")
    public AjaxResult myQuestionBankList() {
        return AjaxResult.success("查询成功", examQuestionBankService.myQuestionBankList(getUserId()));
    }

    @ApiOperation("查询题库进度信息")
    @GetMapping("/getBankProgressInfo/{bankId}")
    public AjaxResult getBankProgressInfo(@PathVariable("bankId") Long bankId) {
        //获取题库信息
        BankProgressInfoVo bankProgressInfoVo = examQuestionBankService.getBankProgressInfo(bankId, getUserId());
        //查询学习天数
        Long studyDays = examQuestionBankService.getStudyDays(bankId, getUserId());
        bankProgressInfoVo.setStudyDays(studyDays != null ? studyDays : 0L);
        //查询用户答题记录统计
        Map<String, Object> answerStats = examQuestionBankService.getUserAnswerStats(bankId, getUserId());
        if (answerStats != null) {
            bankProgressInfoVo.setStudiedCount(((Number) answerStats.get("studied_count")).longValue());
            bankProgressInfoVo.setTotalAnswerCount(((Number) answerStats.get("total_answer_count")).longValue());
            bankProgressInfoVo.setCorrectAnswerCount(((Number) answerStats.get("correct_answer_count")).longValue());
            bankProgressInfoVo.setAccuracyRate(new java.math.BigDecimal(answerStats.get("accuracy_rate").toString()));
        } else {
            bankProgressInfoVo.setStudiedCount(0L);
            bankProgressInfoVo.setTotalAnswerCount(0L);
            bankProgressInfoVo.setCorrectAnswerCount(0L);
            bankProgressInfoVo.setAccuracyRate(java.math.BigDecimal.ZERO);
        }
        
        return AjaxResult.success("查询成功", bankProgressInfoVo);
    }

    @ApiOperation("查询本周答题打卡情况")
    @GetMapping("/getWeeklyCheckIn")
    public AjaxResult getWeeklyCheckIn() {
        WeeklyCheckInVo weeklyCheckIn = examQuestionBankService.getWeeklyCheckIn(getUserId());
        return AjaxResult.success("查询成功", weeklyCheckIn);
    }
}
